jQuery(document)
		.ready(
				function() {
					jQuery("a[name='addCs']")
							.click(function() {
								// 在#cs的div中的ul下添加一个li
									var n = -1
											* Math
													.round(100000 * Math
															.random()); // csID
									jQuery("#cs")
											.find('ul') 
											.append(
													"<li id='cs"
															+ n
															+ "'><table><tr>"
															+ "<td>中文含义:</td><td><input type='text' name='cn'/></td>"
															+ "</tr><tr>"
															+ "<td>例句:</td><td><textarea name='cs'></textarea></td>"
															+ "</tr><tr>"
															+ "<td><a href='#' name='deleteCs' onClick='deleteCs(this)'>删除</a><input type='hidden' name='csID' value='"
															+ n
															+ "'/></td>"
															+ "</tr></table></li>");

									return false;
								});

					jQuery("#submit")
							.click(function() {
								// 组合成一个form，post到wordlib.htm，根据wordID判断method是加或者更新
									// 获取worID,wordEn,phonetics,所有的wordCn和sentence的组合
									var wordID = jQuery("input[name='wordID']")
											.val(); // wordID
									var wordEn = jQuery("input[name='wordEn']")
											.val(); // wordEn
									var phonetics = jQuery(
											"input[name='phonetics']").val(); // phonetics
									var wordLibID = jQuery(
											"input[name='wordLibID']").val();

									if (!wordEn || jQuery.trim(wordEn) <= 0) {
										alert('英文单词不能为空');
										return false;
									}

									var csIDArr = []; // 用于存放csID的数组
									var wordCnArr = []; // 用于存放wordCn的数组
									var sentenceArr = []; // 用于存放sentence的数组

									var j = 0; // 数组索引
									jQuery("#cs")
											.find('li')
											.each(
													function(i) {
														var csID = jQuery(this)
																.find(
																		"input[name='csID']")
																.val();
														var wordCn = jQuery(
																this)
																.find(
																		"input[name='cn']")
																.val();
														var sentence = jQuery(
																this)
																.find(
																		"textarea[name='cs']")
																.val();
														if (wordCn
																&& jQuery
																		.trim(wordCn).length > 0) {
															if (!csID)
																csID = -1;
															csIDArr[j] = csID;
															wordCnArr[j] = wordCn;
															sentenceArr[j++] = sentence;
														}
													});

									var data = {};
									data.wordEn = wordEn;
									data.wordID = wordID ? wordID : -1;
									data.phonetics = phonetics;
									data.csIDs = csIDArr.join("#");
									data.cns = wordCnArr.join("#");
									data.ss = sentenceArr.join("#");
									data.wordLibID = wordLibID;

									data.action = "addAndEdit";

									jQuery
											.ajax( {
												url : "/HealTheWord/word.json",
												type : "post",
												data : data,
												error : function(data) {
													alert("error");
												},
												success : function(data) {
													if (wordID > 0) {
														alert("单词修改成功");
													} else if (wordID < 0) {
														alert("生词添加成功");
													}
													var href = window.location.href; // 链接地址
													var i = href.indexOf('?'); // 问号在链接地址中的位置
													window.location.href = href
															.substring(0, i + 1)
															+ 'method=displayOne&wordLibID='
															+ wordLibID;
													// window.location.href =
													// 'http://localhost:8080/HealTheWord/administrator/wordlib.htm?method=displayOne&wordLibID='
													// + wordLibID;
												}
											});

									return false;
								});

					jQuery("a[name='deleteCs']").click(function() {
						deleteCs(this);
					});

				});

function deleteCs(el) {
	// 获取csID,ajax删除该例句
	csID = parseInt(jQuery(el).next().val());

	if (csID != null && csID > 0) {
		jQuery.ajax( {
			type : "GET",
			url : "/HealTheWord/word.json?action=deleteCs&csID=" + csID,
			dataType : "text",
			error : function(data) {
				alert("error");
			},
			success : function(data) {
				// 删除框
			jQuery("#cs" + csID).remove();
		}
		});
	} else {
		jQuery("#cs" + csID).remove();
	}
	return false;
}